package simco.framework.gui.utils;

import java.awt.Color;
import java.io.FileNotFoundException;

import simco.framework.gui.graph.supp.ViewModeType;
import simco.framework.gui.services.jaxb.ColorType;
import simco.framework.gui.services.jaxb.SimcoGuiCfg;

public class Configuration 
{	
	/**
	 * Jmeno pouziteho fontu pro vykreslovani na platno
	 */
    public static final String FONT_NAME = "Dialog";
    
    /**
     * Minimalni nastavitelna velikost pisma
     */
    public static final int MIN_FONT_SIZE = 9;
    
    /**
     * Maximalni nastavitelna velikost pisma
     */
    public static final int MAX_FONT_SIZE = 16;

    /**
     * Velikost okraju u objektu vykreslovanych na platno
     */
    public static final int INSETS_SIZE = 5;
	
    /**
     * Vychozi pocet iteraci Spring layoutu pro rozmistovani objektu
     */
	public static final int DEFAULT_LO_ITERATION_COUNT = 1000;
	
	/**
	 * Vychozi pocet pokudu o rozmisteni objektu pri nacteni scenare
	 */
	public static final int DEFAULT_LOADLO_ATTEMPS_COUNT = 100;
	
	/**
	 * Vychozi pocet pokudu o rozmisteni objektu pri vyzadani relayoutu
	 */
	public static final int DEFAULT_RELO_ATTEMPS_COUNT = 50;
	
	/**
	 * Vychozi pro zapnuti prerozmisteni po zmene zobrazovaciho modu
	 */
	public static final boolean DEFAULT_RELO_A_VIEW_CHANGE = true;

	/**
	 * Vychozi barva oznacenych objektu
	 */
    public static final Color DEFAULT_COLOR_OF_SELECTED = new Color(160,196,205);

    /**
	 * Vychozi barva objektu pouzitych pri poslednim kroku simulace
	 */
    public static final Color DEFAULT_COLOR_OF_MARKDER = new Color(200, 12, 2);
	
    /**
	 * Vychozi velikost pisma
	 */
    public static final int DEFAULT_FONT_SIZE = 11;
    
	/**
	 * Vychozi zobrazovaci mod grafu
	 */
    public static final ViewModeType DEFAULT_VIEW_MODE = ViewModeType.STANDARD;
    
	/**
	 * Vychozi delka kalendare
	 */
    public static final int DEFAULT_HISTORY_LENGTH = 500;
    
	/**
	 * Vychozi zpozdeni simulacniho kroku
	 */
    public static final int DEFAULT_STEP_DELAY = 500;
    
	/**
	 * Minimalni doba zpozdeni simulacniho kroku
	 */
    public static final int MIN_STEP_DELAY = 0;
    
    /**
	 * Maximalni doba zpozdeni simulacniho kroku
	 */
    public static final int MAX_STEP_DELAY = 5000;
    
    /**
	 * Vychozi adresar pro umisteni logovaciho souboru jadra
	 */
    public static final String DEFAULT_LOG_PATH = "";
    
    /**
     * Pocet iteraci Spring layoutu pro rozmistovani objektu
     */
	private static int layoutIterationsCount;
	
	/**
	 * Pocet pokusu pri rozmistovani objektu po nacteni scenare
	 */
	private static int loadLayoutAttempsCount;
	
	/**
	 * Pocet pokusu pri rozmistovani objektu pri prerozmistovani grafu
	 */
	private static int reLayoutAttempsCount;
	
	/**
	 * Barva vybranych objektu
	 */
	private static Color colorOfSelected;
	
	/**
	 * Barva objektu pouzitych v poslednim sim. kroku
	 */
	private static Color colorOfMarked;
	
	/**
	 * Velikost pisma na platne
	 */
	private static int fontSize;
	
	/**
	 * Aktualni lokalizace
	 */
	private static String locality;
	
	/**
	 * Hodnota true indikuje, ze po kazde zmene zobrazovaciho modu je graf prerozmisten.
	 */
	private static boolean reloAViewModeChange;
	
	/**
	 * Aktualni zobrazovaci mod grafu
	 */
	private static ViewModeType viewMode;
	
	/**
	 * Delka historie kalendare
	 */
	private static int historyLength;
	
	/**
	 * Aktualni zpozdeni sim. kroku
	 */
	private static int stepDelay;
	
	/**
	 * Aktualni cesta k souboru, do ktereho jadro loguje
	 */
	private static String logPath;
	
	/**
	 * Indikuje, zda-li je aktualni konfigurace ulozena
	 */
	private static boolean wasConfigChanged;
	
	/**
	 * Obsahuje hodnotu true pokud nebyl nalezen konfiguracni souboru
	 */
	private static boolean noConfigFound;
	
	/**
	 * Obsahuje hodnotu true pokud konfiguracni souboru obsahuje chybu
	 */
	private static boolean errorInConfig;
	
	/**
	 * Cesta k adresari, ve kterem je ulozena spoustena GUI aplikace
	 */
	private static String applicationPath;

	static 
	{
		resolveAppPath();
		inicialization();
	}
	
	/**
	 * Inicializuje nastaveni vsech polozek konfigurace. Nacte nastaveni
	 * z konfiguracniho souboru. Pokud konfiguracni soubor neni k dispozici,
	 * pak provede nastaveni defaultnich hodnot.
	 * 
	 */
	private static void inicialization()
	{		
		noConfigFound = false;
		errorInConfig = false;
		reloAViewModeChange = DEFAULT_RELO_A_VIEW_CHANGE;
		stepDelay = DEFAULT_STEP_DELAY;

		SimcoGuiCfg sgc = null;
		try 
		{
			XmlConfigUtil.validateConfig();
			sgc = XmlConfigUtil.loadConfigurationObject();
		} 
		catch (FileNotFoundException e) 
		{
			noConfigFound = true;
			LoggingUtil.getGuiLogger().error("", e);
		}
		catch (Exception e) 
		{
			errorInConfig = true;
			LoggingUtil.getGuiLogger().error("", e);
		}
		
		if(noConfigFound == true || errorInConfig == true)
		{
			// konfiguracni soubor nebyl nalezen nebo obsahuje chyby
			layoutIterationsCount = DEFAULT_LO_ITERATION_COUNT;
			loadLayoutAttempsCount = DEFAULT_LOADLO_ATTEMPS_COUNT;
			reLayoutAttempsCount = DEFAULT_RELO_ATTEMPS_COUNT;		
			fontSize = DEFAULT_FONT_SIZE;
			locality = "cs";
			viewMode = DEFAULT_VIEW_MODE;
			historyLength = DEFAULT_HISTORY_LENGTH;
			colorOfMarked = DEFAULT_COLOR_OF_MARKDER;
			colorOfSelected = DEFAULT_COLOR_OF_SELECTED;
			logPath = DEFAULT_LOG_PATH;
		}
		else
		{
			// nacteni nastaveni z konfiguracniho souboru
			layoutIterationsCount = sgc.getSpringIterations();			
			loadLayoutAttempsCount = sgc.getAttempsOnLoad();
			reLayoutAttempsCount = sgc.getAttempsOnRelayout();
			logPath = sgc.getCoreLogPath();
			fontSize = sgc.getFontSize();
			locality = sgc.getLocalization();			
			viewMode = ViewModeType.decodeType(sgc.getDefaultViewMode());
			historyLength = sgc.getHistoryLength();
			ColorType mc = sgc.getMarkColor();
			colorOfMarked = new Color(mc.getRed(), mc.getGreen(), mc.getBlue());
			ColorType sc = sgc.getSelectionColor();
			colorOfSelected = new Color(sc.getRed(), sc.getGreen(), sc.getBlue());			
		}
		
		wasConfigChanged = false;
	}
	
	/**
	 * Zjistil cestu k adresari, ze ktereho je aplikace spustena.
	 */
	private static void resolveAppPath()
	{
		String classPath = XmlConfigUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath(); 
		applicationPath = classPath.substring(0, classPath.lastIndexOf("bin"));
	}
	
	/*
	 * ========================================
	 * ACCESORY:
	 * (k vyse uvedenym atributum tridy)
	 * ========================================
	 */
	
	public static int getLayoutIterationsCount() 
	{
		return layoutIterationsCount;
	}

	public static  void setLayoutIterationsCount(int layoutIterationsCount) 
	{
		wasConfigChanged = true;
		Configuration.layoutIterationsCount = layoutIterationsCount;
	}

	public static  int getLoadLayoutAttempsCount() 
	{
		return loadLayoutAttempsCount;
	}

	public static  void setLoadLayoutAttempsCount(int loadLayoutAttempsCount) 
	{
		
		Configuration.loadLayoutAttempsCount = loadLayoutAttempsCount;
	}

	public static  int getReLayoutAttempsCount() 
	{
		return reLayoutAttempsCount;
	}

	public static  void setReLayoutAttempsCount(int reLayoutAttempsCount) 
	{
		wasConfigChanged = true;
		Configuration.reLayoutAttempsCount = reLayoutAttempsCount;
	}

	public static Color getColorOfSelected() {
		return colorOfSelected;
	}

	public static void setColorOfSelected(Color colorOfSelected) 
	{
		wasConfigChanged = true;
		Configuration.colorOfSelected = colorOfSelected;
	}

	public static Color getColorOfMarked() {
		return colorOfMarked;
	}

	public static void setColorOfMarked(Color colorOfMarked) 
	{
		wasConfigChanged = true;
		Configuration.colorOfMarked = colorOfMarked;
	}

	public static int getFontSize() {
		return fontSize;
	}

	public static void setFontSize(int fontSize) 
	{
		wasConfigChanged = true;
		Configuration.fontSize = fontSize;
	}

	public static boolean isReloAViewModeChange() {
		return reloAViewModeChange;
	}

	public static void setReloAViewModeChange(boolean reloAViewModeChange) 
	{
		wasConfigChanged = true;
		Configuration.reloAViewModeChange = reloAViewModeChange;
	}

	public static String getLocality() 
	{
		return locality;
	}
	
	public static void setLocality(String locality) {
		Configuration.locality = locality;
	}

	public static ViewModeType getViewMode() {
		return viewMode;
	}

	public static void setViewMode(ViewModeType viewMode) 
	{
		wasConfigChanged = true;
		Configuration.viewMode = viewMode;
	}
	
	public static int getHistoryLength() {
		return historyLength;
	}

	public static void setHistoryLength(int historyLength) 
	{
		wasConfigChanged = true;
		Configuration.historyLength = historyLength;
	}
	
	public static int getStepDelay() {
		return stepDelay;
	}

	public static void setStepDelay(int stepDelay) {
		Configuration.stepDelay = stepDelay;
	}

	public static String getLogPath() {
		return logPath;
	}

	public static void setLogDirectory(String logDirectory) 
	{
		wasConfigChanged = true;
		Configuration.logPath = logDirectory;
	}

	public static boolean isWasConfigChanged() 
	{
		return wasConfigChanged;
	}
	
	public static void configurationSaved()
	{
		wasConfigChanged = false;
	}

	public static boolean isNoConfigFound() {
		return noConfigFound;
	}

	public static boolean isErrorInConfig() 
	{
		return errorInConfig;
	}

	public static String getApplicationPath() {
		return applicationPath;
	}
	
}


